package cn.com.nes.site.action.contract;

import cn.com.nes.common.util.DBUtils;
import cn.com.nes.site.entity.DataReq;
import cn.com.nes.site.entity.DataRes;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.module.service.contract.ContractService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller("contract")
@RequestMapping("/contract")
public class ContractAction {

    @Resource
    private ContractService service;

    /**
     * 查询合同列表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getContractList")
    public @ResponseBody PageData getContractList(HttpServletRequest request, HttpServletResponse response) {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();

        res = service.getContractList(req);
        return res;
    }

    /**
     * 合同录入
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/saveContract")
    @ResponseBody
    public PageData saveContract(HttpServletRequest request, HttpServletResponse response) {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();

        res = service.saveContract(req);
        return res;
    }


    @RequestMapping("/generateContractNumber")
    @ResponseBody
    public PageData generateContractNumber(HttpServletRequest request, HttpServletResponse response) {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();

        String contractNumber = service.generateContractNumber(req);

        Map<String, Object> map = new HashMap<>();
        map.put("contractNumber", contractNumber);
        res = new PageData();
        res.setData(map);
        res.setRetCode(0);
        res.setRetMessage("OK");
        return res;
    }

    /**
     * 终止合同
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/terminateContract")
    @ResponseBody
    public PageData terminateContract(HttpServletRequest request, HttpServletResponse response) {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();

        res = service.terminateContract(req);
        return res;
    }

    /**
     * 删除合同
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/deleteContract")
    @ResponseBody
    public PageData deleteContract(HttpServletRequest request, HttpServletResponse response) {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();

        res = service.deleteContract(req);
        return res;
    }

    /**
     * 审核合同
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/auditContract")
    @ResponseBody
    public PageData auditContract(HttpServletRequest request, HttpServletResponse response) {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();

        res = service.auditContract(req);
        return res;
    }

    /**
     * 获取合同类型
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getContractTypes")
    @ResponseBody
    public PageData getContractTypes(HttpServletRequest request, HttpServletResponse response) {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();

        res = service.getContractTypes(req);
        return res;
    }

    /**
     * 保存合同类型
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/saveContractType")
    @ResponseBody
    public PageData saveContractType(HttpServletRequest request, HttpServletResponse response) {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();

        res = service.saveContractType(req);
        return res;
    }

    /**
     * 删除合同类型
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/deleteContractType")
    @ResponseBody
    public PageData deleteContractType(HttpServletRequest request, HttpServletResponse response) {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();

        res = service.deleteContractType(req);
        return res;
    }

}
